import 'package:flutter/material.dart';
import 'package:xws/models/goods/comment.dart';
import 'package:xws/models/goods/comment_obj.dart';
import 'package:xws/utils/data_util.dart';
import 'package:xws/utils/theme_util.dart';
import 'package:xws/widgets/app_img.dart';
import 'package:xws/widgets/app_list_tile.dart';

/// 评论项
class CommentItem extends StatefulWidget {
  const CommentItem({
    Key? key,
    required this.commentObj,
    this.tapComment,
  }) : super(key: key);

  @override
  _CommentItemState createState() => _CommentItemState();

  final CommentObj commentObj;
  final Function()? tapComment;
}

class _CommentItemState extends State<CommentItem> {
  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        _buildCommentItem(widget.commentObj),
        _replayList,
      ],
    );
  }

  Widget get _replayList {
    return Padding(
      padding: const EdgeInsets.only(left: 40, bottom: 30),
      child: Column(
        children: List<Widget>.generate(
          widget.commentObj.replyList.length,
          (int index) => Padding(
            padding: const EdgeInsets.only(bottom: 10),
            child: _buildCommentItem(widget.commentObj.replyList[index]),
          ),
        ),
      ),
    );
  }

  Widget _buildCommentItem(Comments c) {
    final bool isComment = c is CommentObj;

    return AppListTile(
      onTap: widget.tapComment,
      padding: const EdgeInsets.only(bottom: 20),
      crossAxisAlignment: CrossAxisAlignment.start,
      leading: Padding(
        padding: const EdgeInsets.only(right: 10),
        child: AppImg.fitCover(
          url: c.avatarUrl,
          width: isComment ? 30 : 20,
          height: isComment ? 30 : 20,
          radius: isComment ? 15 : 10,
        ),
      ),
      title: Padding(
        padding: const EdgeInsets.only(bottom: 4),
        child: Row(
          children: <Widget>[
            Flexible(
              child: Text(
                c.nickName,
                maxLines: 1,
                style: TextStyle(
                  fontSize: 13,
                  color: black(context).withOpacity(0.4),
                  fontWeight: FontWeight.bold,
                ),
              ),
            ),
            Text(
              ' • ' + commentDate(c.createdAt),
              maxLines: 1,
              style: TextStyle(
                fontSize: 11,
                color: black(context).withOpacity(0.4),
              ),
            ),
          ],
        ),
      ),
      subtitle: Text(
        c.content ?? '',
        style: TextStyle(
          fontSize: 13,
          color: black(context).withOpacity(0.8),
          fontWeight: FontWeight.w500,
        ),
      ),
      // trailing: Padding(
      //   padding: const EdgeInsets.only(left: 20),
      //   child: Column(
      //     children: <Widget>[
      //       Icon(
      //         IR.un_like,
      //         size: 14,
      //         color: black(context).withOpacity(0.3),
      //       ),
      //       const SizedBox(height: 4),
      //       Text(
      //         '126',
      //         style: TextStyle(
      //           fontSize: 9,
      //           fontWeight: FontWeight.bold,
      //           color: black(context).withOpacity(0.3),
      //         ),
      //       ),
      //     ],
      //   ),
      // ),
    );
  }
}
